﻿#include "addaifaceform.h"
#include "ui_addaifaceform.h"
#include <QJsonObject>
#include "httpclient.h"
#include "iconhelper.h"
#include "myhelper.h"
#include "httpclient.h"
#include <QUrlQuery>
#include <QHttpPart>
#include <QHttpMultiPart>

AddAifaceForm::AddAifaceForm(const QString &ip, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddAifaceForm),
    m_ip(ip)
{
    ui->setupUi(this);
    this->initform();
}

AddAifaceForm::~AddAifaceForm()
{
    delete ui;
}

void AddAifaceForm::initform()
{

    this->setWindowTitle("添加员工信息");
    QHBoxLayout *pSearchLayout = new QHBoxLayout(ui->led_image_path);
    pSearchLayout->addStretch();
    pSearchLayout->setSpacing(0);
    pSearchLayout->setContentsMargins(0, 0, 0, 0);
    QPushButton *search = new QPushButton(this);
    search->resize(ui->led_image_path->height(), ui->led_image_path->height());
    search->setFocusPolicy(Qt::NoFocus);
    search->setCursor(QCursor(Qt::PointingHandCursor));
    search->setFlat(true);
    search->setStyleSheet("background-color: rgb(9, 19, 50);color: rgb(255, 255, 255);"
                          "width:24px;height:24px;margin:2px;");
    QPixmap pix = IconHelper::Instance()->getPixmap("#255990", 0xf07c, 30, 28, 28);
    search->setIcon(pix);
    pSearchLayout->addWidget(search);
    ui->led_image_path->setTextMargins(0, 0,  ui->led_image_path->height(), 0);
    ui->led_image_path->setPlaceholderText("人脸图片");
    connect(search, &QPushButton::clicked, this, [ = ]
    {
        //QString filepath = QFileDialog::getOpenFileName(this, tr("上传员工图片"), ".", tr(" (*.jpg);;(*.png)"));
        QString filepath = QFileDialog::getOpenFileName(this, tr("上传员工图片"), ".", tr(" (*.jpg)"));
        ui->led_image_path->setText(filepath);
    });


    QHBoxLayout *pSearchLayout1 = new QHBoxLayout(ui->led_uuid);
    pSearchLayout1->addStretch();
    pSearchLayout1->setSpacing(0);
    pSearchLayout1->setContentsMargins(0, 0, 0, 0);
    QPushButton *create = new QPushButton(this);
    create->resize(ui->led_uuid->height(), ui->led_uuid->height());
    create->setFocusPolicy(Qt::NoFocus);
    create->setCursor(QCursor(Qt::PointingHandCursor));
    create->setFlat(true);
    create->setStyleSheet("background-color: rgb(9, 19, 50);color: rgb(255, 255, 255);"
                          "width:24px;height:24px;margin:2px;");
    QPixmap pix1 = IconHelper::Instance()->getPixmap("#255990", 0xf01e, 30, 28, 28);
    create->setIcon(pix1);
    pSearchLayout1->addWidget(create);
    ui->led_uuid->setTextMargins(0, 0,  ui->led_uuid->height(), 0);
    ui->led_uuid->setPlaceholderText("唯一标识");
    connect(create, &QPushButton::clicked, this, [ = ]
    {
        QString uuid = QUuid::createUuid().toString();
        ui->led_uuid->setText(uuid.remove(0, 1).remove(uuid.length() - 2, 1));
    });
}

void AddAifaceForm::on_btn_upload_clicked()
{
    QJsonObject body;
    body.insert("uuid", ui->led_uuid->text());
    body.insert("face_image_path", ui->led_image_path->text());
    body.insert("name", ui->led_name->text());
    body.insert("work_number", ui->led_work_number->text());
    body.insert("id_number", ui->led_id_number->text());
    body.insert("phone", ui->led_phone->text());
    body.insert("department", ui->led_department->text());
    body.insert("memo_fir", ui->led_memo_fir->text());
    body.insert("memo_sec", ui->led_memo_sec->text());
    body.insert("memo_thi", ui->led_memo_thi->text());
    QJsonDocument jsondoc;
    jsondoc.setObject(body);
    QString request = QString(jsondoc.toJson(QJsonDocument::Compact));
    QString request_path = QString("http://%1:8800/api/face").arg(m_ip);
    HttpClient(request_path).success([this](const QString & response)
    {
        if(QMessageBox::information(nullptr, tr("通知"), tr("员工信息上传成功"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            return;
        }
    }).json(request).post();
}


void AddAifaceForm::on_btn_upload_image_clicked()
{
    QString request_image_path = QString("http://%1:8800/upload/face?uuid=%2").arg(m_ip).arg(ui->led_uuid->text());
    HttpClient(request_image_path).success([this](const QString & response)
    {
        qDebug() << response;
        if(QMessageBox::information(nullptr, tr("通知"), tr("员工图片上传成功"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            emit sig_clear();
            this->close();
        }
    }).upload(ui->led_image_path->text());
//    QNetworkAccessManager* m_pNetWorkManager = new QNetworkAccessManager(this);
//    QNetworkReply* reply;
//    QNetworkRequest request;
//    request.setUrl(request_image_path);
//    QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//    QHttpPart imagePart;
//    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));//如果是png图片填image/png
//    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("multipart/form-data; name=\"image\"; filename=\"123.jpg\""));
//    imagePart.setRawHeader("content-type", "multipart/form-data"); // 添加任何你喜欢的 headers
//    QFile* file = new QFile(ui->led_image_path->text());
//    file->open(QIODevice::ReadOnly);
//    imagePart.setBodyDevice(file);
//    multiPart->append(imagePart);

//    reply = m_pNetWorkManager->post(request, multiPart);
//    QObject::connect(reply, &QNetworkReply::finished, [ = ]
//    {
//        multiPart->deleteLater(); // 释放资源: multiPart + file
//        qDebug() << "upload success";
//        qDebug() << reply->errorString();
//        QTextStream in(reply);
//        QString result;
//        //in.setCodec(charset.toUtf8());

//        while (!in.atEnd())
//        {
//            result += in.readLine();
//        }
//        qDebug() << result;
//    });
}
